<template>
  <div
    class="time-line-wrap"
    :data-left-number="wgybVisable ? '560px' : '450px'"
  >
    <div class="btn-type-wrap">
      <div
        class="btn-type-item"
        v-for="(item, index) in btnArr"
        :class="dateActive === index && 'active'"
        :key="item.dataTime"
        @click="changeBtn(item, index)"
      >
        {{ item.dataTime.substring(5) }}
      </div>
    </div>
    <div class="time-wrap">
      <div class="retreat" @click="retreat()">
        <img src="../images/Previous.png" alt="" />
      </div>
      <div class="play suspend" @click="play()">
        <img v-if="timerId" src="../images/suspend.png" alt="" />
        <img v-if="!timerId" src="../images/play.png" alt="" />
      </div>
      <div class="forward" @click="forward()">
        <img src="../images/Next.png" alt="" />
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import dayjs from 'dayjs'
const wgybVisable = inject('wgybVisable')
import apiList from '@/pages/forestry/api/api'
import { fireInsuranceStore } from '@/pages/forestry/store/store'
const fireStore = fireInsuranceStore()

let btnArr = ref()
let timerId = inject('timerId')
let dateActive = ref(0)
const isForward = ref(false)
const isRetreat = ref(false)

const initData = async () => {
  const { data } = await apiList.getGridPointForestFireDangerLevel()
  btnArr.value = data
}

const play = () => {
  //播放暂停按钮
  if (btnArr.length < 2) return
  if (!timerId.value) {
    playTimer()
  } else {
    closeplay()
  }
}
const closeplay = () => {
  //关闭播放
  clearInterval(timerId.value)
  timerId.value = null
}
const playTimer = () => {
  isRetreat.value = false
  isForward.value = false
  //执行播放定时器
  timerId.value = setInterval(() => {
    dateActive.value++
    fireStore.timerIndex++
    if (dateActive.value == btnArr.value.length) {
      dateActive.value = 0
      fireStore.timerIndex = 0
    }
  }, 2000)
}
const retreat = () => {
  //退后一个
  //先清除定时器 修改当前下标 重新执行定时器
  if (isRetreat.value) return
  let flag = timerId.value
  if (flag) {
    clearInterval(timerId.value)
    timerId.value = 0
    dateActive.value -= 1
    fireStore.timerIndex -= 1
  } else {
    if (!dateActive.value) {
      isRetreat.value = true
      return
    }
    dateActive.value -= 1
    fireStore.timerIndex -= 1
  }
}
const forward = () => {
  //前进一个
  //先清除定时器 修改当前下标 重新执行定时器
  if (isForward.value) return
  let flag = timerId.value
  if (flag) {
    clearInterval(timerId.value)
    timerId.value = 0
    dateActive.value += 1
    fireStore.timerIndex += 1
  } else {
    dateActive.value += 1
    fireStore.timerIndex += 1
    if (dateActive.value == btnArr.value.length) {
      dateActive.value = 0
      fireStore.timerIndex = 0
    }
  }
}
const changeBtn = (item, index) => {
  dateActive.value = index
  fireStore.timerIndex = index
}
watch(
  () => window.location.hash == '#/forestry',
  () => {
    clearInterval(timerId.value)
    timerId.value = null
  },
  { deep: true, immediate: true }
)
onMounted(() => {
  initData()
})
</script>
<style lang="scss" scoped>
.time-line-wrap {
  --left-number: calc(var(--left-number-value) * 1px);
  position: absolute;
  left: 50%;
  top: 85px;
  transform: translateX(-50%);
  width: 430px;
  height: 70px;
  background: rgba(0, 13, 35, 0.59);
  box-shadow: 0px 0px 6px 1px rgba(0, 204, 255, 0.77),
    inset 0px 0px 14px 1px #00ccff;
  border-radius: 4px 4px 4px 4px;
  border: 1px solid #2c96cd;
  z-index: 1200;
  cursor: default;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 0 15px;
  box-sizing: border-box;
  font-size: 16px;
  font-family: Microsoft YaHei-Regular, Microsoft YaHei;
  font-weight: 400;
  color: #ffffff;
  transition: all 0.5s;
  .btn-type-wrap {
    width: 288px;
    height: 36px;
    background: rgba(0, 13, 35, 0.68);
    box-shadow: inset 0px 0px 14px 1px #00ccff;
    border-radius: 22px 22px 22px 22px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .btn-type-item {
      background: none;
      cursor: pointer;
      color: #ffffff;
      float: left;
      width: 65px;
      text-align: center;
      &.active {
        width: 65px;
        height: 36px;
        background: #006dff;
        box-shadow: inset 0px 0px 14px 1px #00ccff;
        border-radius: 22px 22px 22px 22px;
        text-align: center;
        line-height: 36px;
      }
    }
  }
  .time-wrap {
    // display: flex;
    // justify-content: flex-start;
    // align-items: center;
    width: 102px;
    margin-left: 5px;
    overflow: hidden;
    .retreat,
    .play,
    .suspend,
    .forward {
      width: 24px;
      height: 24px;
      background: #006dff;
      border-radius: 4px 4px 4px 4px;
      cursor: pointer;
      color: #fff;
      font-size: 14px;
      text-align: center;
      line-height: 24px;
      margin-right: 8px;
      float: left;
      img {
        width: 8px;
        height: 11px;
      }
    }
    .play,
    .suspend {
      img {
        width: 13px;
        height: 13px;
      }
    }
    .forward {
      margin-right: 14px;
    }
    .prev {
      width: 19px;
      height: 34px;
      background: #4056cc;
      margin: 0 5px;
      cursor: pointer;
      color: #fff;
      font-size: 16px;
      text-align: center;
      line-height: 34px;
    }

    .next {
      width: 19px;
      height: 34px;
      background: #4056cc;
      margin: 0 5px;
      cursor: pointer;
      color: #fff;
      font-size: 16px;
      text-align: center;
      line-height: 34px;
      transform: rotate(180deg);
    }
  }
  .time-ticp-wrap {
    width: 489px;
    height: 60px;
    float: left;
    overflow: hidden;
    .date {
      width: 100%;
      text-align: center;
      line-height: 1.5;
    }
    .time-ticp {
      width: 525px;
      height: 34px;
      .progress-bar {
        width: 489px;
        height: 10px;
        background: #1b3a52;
        border-radius: 4px 4px 4px 4px;
        .progress {
          width: 100%;
          height: 100%;
          background: linear-gradient(180deg, #00fff7 0%, #006fff 100%);
          border-radius: 4px 4px 4px 4px;
          transition: width 0.9s ease;
        }
      }
      .item-wrap {
        .date-time {
          width: 489px;
          display: flex;
          justify-content: space-between;
          .item {
            // width: 15.5px;
            box-sizing: border-box;
            // margin-right: 5px;
            margin-top: 8px;
            height: 18px;
            font-size: 14px;
            font-family: Microsoft YaHei-Regular, Microsoft YaHei;
            font-weight: 400;
            color: rgba(255, 255, 255, 0.59);
            line-height: 16px;
            // display: inline-block;
          }
        }
      }
    }
  }
}

.time-line-wrap[data-left-number='560px'] {
  --left-number-value: 590;
}

.time-line-wrap[data-left-number='450px'] {
  --left-number-value: 450;
}
</style>
